/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webaccess.retailws.Action;

import com.webaccess.retailws.Bean.BrandBean;
import com.webaccess.retailws.Bean.CategoryBean;
import com.webaccess.retailws.Bean.DealBean;
import com.webaccess.retailws.Bean.ProductBean;
import com.webaccess.retailws.Delegate.AdminDelegate;
import com.webaccess.retailws.Delegate.BrandDelegate;
import com.webaccess.retailws.Delegate.CategoryDelegate;
import com.webaccess.retailws.Delegate.DealDelegate;
import com.webaccess.retailws.Delegate.MenuDelegate;
import com.webaccess.retailws.Delegate.ProductDelegate;
import com.webaccess.retailws.Util.CategoryUtil;
import com.webaccess.retailws.Util.CommonUtil;
import com.webaccess.retailws.Util.menuUtil;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author Owner
 */
public class MenuDispatchAction extends DispatchAction {

    private String SUCCESS = "success";
    CategoryDelegate categDelegate = new CategoryDelegate();
    BrandDelegate brandDelegateObj = new BrandDelegate();
    ProductDelegate prodDelegate = null;
    MenuDelegate menuDelegateObj = null;
    DealDelegate dealDelegateObj = new DealDelegate();
    CategoryUtil categUtil = new CategoryUtil();
    CommonUtil commUtil = new CommonUtil();
    menuUtil menuUtilObj = null;
    ActionMessages errors = null;
    CategoryBean categBean = null;
    BrandBean brandBeanObj = null;
    ProductBean prodBean = null;
    AdminDelegate admnDelegate = null;

    public ActionForward showOnlineStore(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        commUtil.getTopRightListMap(request);
        request.setAttribute("categoryListWithImage", categDelegate.getMainCategoryList());
        SUCCESS = "";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward dealofTheDay(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        prodBean = new ProductBean();
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("subCategList", categDelegate.getSubCategoryLHMap());
            DealBean[] dealList = dealDelegateObj.getAllDeal();
            request.setAttribute("dealList", dealList);

//        request.setAttribute("productList",dealList. )
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            SUCCESS = "showdealoftheday";
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showAdminHome(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        admnDelegate = new AdminDelegate();
        commUtil.getTopRightListMap(request);

        SUCCESS = "showadminhome";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward showHomeBeforeLogin(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        prodBean = new ProductBean();
        prodDelegate = new ProductDelegate();
        try {
            commUtil.getTopRightListMap(request);
            ProductBean[] prodMobileBeanList = prodDelegate.getProductByCategory(1L);
            request.setAttribute("productMobileList", prodMobileBeanList);

            ProductBean[] prodBookBeanList = prodDelegate.getProductByCategory(5L);
            request.setAttribute("productBookList", prodBookBeanList);

            ProductBean[] prodCameraBeanList = prodDelegate.getProductByCategory(127L);
            request.setAttribute("productCameraList", prodCameraBeanList);
            
            SUCCESS = "homepageshow";
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }

    }

    public ActionForward showAboutUs(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil.getTopRightListMap(request);
        SUCCESS = "showaboutus";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward showAllProducts(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil.getTopRightListMap(request);

        SUCCESS = "showallproducts";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward showSearchPage(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            commUtil.getTopRightListMap(request);
            request.setAttribute("brandMap", brandDelegateObj.getBrandLHMap());
            request.setAttribute("mainCategoryMap", categDelegate.getCategoryLHMap(0));
            SUCCESS = "searchpageshow";
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }

    public ActionForward showMyAccount(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession loginSession = request.getSession(true);
        commUtil.getTopRightListMap(request);
        Long userId = Long.parseLong(loginSession.getAttribute("loginUserId").toString());
        Long roleId = Long.parseLong(loginSession.getAttribute("loginRoleId").toString());
        if (userId > 0 && roleId > 0) {
            SUCCESS = "showmyaccount";
        } else {
            SUCCESS = "showlogin";
        }
        return mapping.findForward(SUCCESS);
    }

    public ActionForward showCart(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil.getTopRightListMap(request);

        SUCCESS = "showcart";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward showContactUs(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        commUtil.getTopRightListMap(request);
        saveToken(request);
        SUCCESS = "showcontactus";
        return mapping.findForward(SUCCESS);
    }

    public ActionForward getCategory(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        try {
            commUtil.getTopRightListMap(request);
            HttpSession loginSession = request.getSession(true);
//            System.out.println(loginSession.getAttribute("loginUserId"));
//            System.out.println(loginSession.getAttribute("loginRoleId"));
            if (loginSession.getAttribute("loginUserId") == null && loginSession.getAttribute("loginRoleId") == null) {
                Random randGuest = new Random(System.currentTimeMillis());
                loginSession.setAttribute("loginUserId", Math.abs(randGuest.nextInt()));
                loginSession.setAttribute("loginRoleId", 0);
                loginSession.setAttribute("loginUsername", "Guest");
            }


            SUCCESS = "showcategory";
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            return mapping.findForward(SUCCESS);
        }
    }
}
